package com.catering.service.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.catering.dao.*;
import com.catering.entity.*;
import com.catering.entity.vo.CateringGoodsVo;
import com.catering.service.*;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FrontService {
	
	@Autowired
	private CateringGoodsDao cateringGoodsDao;
	@Autowired
	private CateringGoodsService cateringGoodsService;
	@Autowired
	private CateringSpacesDao cateringSpacesDao;
	@Autowired
	private CateringSpacesChildDao spacesChildDao;
	@Autowired
	private CateringSpacesService spacesService;
	@Autowired
	private CateringSpacesChildService spacesChildService;
	@Autowired
	private CateringCouponDao cateringCouponDao;
	@Autowired
	private CateringDiscussDao cateringDiscussDao;
	@Autowired
	private CateringUserDao cateringUserDao;
	@Autowired
	private CateringOrderDao cateringOrderDao;
	@Autowired
	private CateringOrderService cateringOrderService;
	@Autowired
	private CateringOrderCartDao cateringOrderCartDao;
	@Autowired
	private CateringCartService cateringCartService;
	@Autowired
	private CateringAddressService cateringAddressService;
	@Autowired
	private CateringUserCouponService cateringUserCouponService;
	@Autowired
	private CateringIntegralService cateringIntegralService;
	@Autowired
	private CateringOrderLogService cateringOrderLogService;
	@Autowired
	private CateringFeedbackService cateringFeedbackService;
	@Autowired
	private CateringModuleService cateringModuleService;

	public ResultData cateringGoodsSelect(Map<String,String> map) {
		return cateringGoodsService.select(map);
	}
	//新增父级规格
	public ResultData cateringGoodsInsert(Map map) {
		map.put("goodsNo",getCardNo("LXX"));
		return cateringGoodsService.insert(map);
	}

	private String getCardNo(String no) {
		return CommonUtils.getRandomByTime(no);
	}

	//父级规格修改
	public ResultData cateringGoodsUpdate(Map map) {
		try {
			CateringGoods vo = JSON.parseObject(JSON.toJSONString(map),CateringGoods.class);
			CateringGoods as = cateringGoodsDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringGoodsDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}
	//父级规格删除
	public ResultData cateringGoodsDelete(Map map) {
		try {
			CateringGoods vo = JSON.parseObject(JSON.toJSONString(map),CateringGoods.class);
			CateringGoods as = cateringGoodsDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
			cateringGoodsDao.deleteById(as.getSysId());
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
		}
	}
	public ResultData cateringSpacesSelect(Map map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"t1");
			wrapper.orderByAsc("t1.goods_id");
			wrapper.orderByAsc("t1.sort");
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringSpaces> page =new Page<CateringSpaces>(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("limit").toString()));
				IPage<CateringSpaces> pageList = cateringSpacesDao.selectPageList(page, wrapper);
				Integer count = cateringSpacesDao.selectPageCount(wrapper);
				List<CateringSpaces> list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				List<CateringSpaces> list = cateringSpacesDao.selectNoPageList(wrapper);
				return ResultData.success(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
		}
	}
	public ResultData spacesChildSelect(Map map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"t1");
			wrapper.orderByAsc("t1.goods_id");
			wrapper.orderByAsc("t1.sort");
			List<CateringSpaces> list = cateringSpacesDao.selectNoPageList(wrapper);
			for(CateringSpaces spaces : list){
				List<CateringSpacesChild> childList = spacesChildDao.selectList(new LambdaQueryWrapper<CateringSpacesChild>().eq(CateringSpacesChild::getParentId,spaces.getSysId()));
				spaces.setChildList(childList);
			}
			return ResultData.success(list);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
		}
	}
	//新增父级规格
	public ResultData cateringSpacesInsert(Map map) {
		return spacesService.insert(map);
	}
	//父级规格修改
	public ResultData cateringSpacesUpdate(Map map) {
		return spacesService.update(map);
	}
	//父级规格删除
	public ResultData cateringSpacesDelete(Map map) {
		return spacesService.delete(map);
	}
	
	public ResultData childSpacesSelect(Map<String,String> map) {
		return spacesChildService.select(map);
	}
	//新增子级规格
	public ResultData childSpacesInsert(Map map) {
		return spacesChildService.insert(map);
	}
	//子级规格修改
	public ResultData childSpacesUpdate(Map map) {
		return spacesChildService.update(map);
	}
	//子级规格删除
	public ResultData childSpacesDelete(Map map) {
		return spacesChildService.delete(map);
	}

	public ResultData cateringCouponSelect(Map map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"");
			wrapper.orderByAsc("status");
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringCoupon> page =new Page<CateringCoupon>(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("limit").toString()));
				IPage<CateringCoupon> pageList = cateringCouponDao.selectPage(page, wrapper);
				Integer count = cateringCouponDao.selectCount(wrapper);
				List<CateringCoupon> list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				List<CateringCoupon> list = cateringCouponDao.selectList(wrapper);
				return ResultData.success(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
		}
	}
	//新增商品优惠卷
	public ResultData cateringCouponInsert(Map map) {
		try {
			CateringCoupon vo = JSON.parseObject(JSON.toJSONString(map),CateringCoupon.class);
			if(CommonUtils.checkNoNull(vo.getParValue())) {
				CateringCoupon bt = cateringCouponDao.selectOne(new LambdaQueryWrapper<CateringCoupon>().eq(CateringCoupon::getParValue, vo.getParValue()));
				if(bt != null) return ResultData.error(ResultEnum.SAME_DATA.getCode(),"优惠卷“"+vo.getParValue()+"”"+ResultEnum.SAME_DATA.getMsg());
			}else return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),"优惠卷"+ResultEnum.REQ_DATA_ERROR.getMsg());
			vo.setCreateTime(CommonUtils.getTime(19));
			vo.setCouponNo(getCardNo("YHQ"));
			cateringCouponDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}
	//商品优惠卷修改
	public ResultData cateringCouponUpdate(Map map) {
		try {
			CateringCoupon vo = JSON.parseObject(JSON.toJSONString(map),CateringCoupon.class);
			if(CommonUtils.checkNoNull(vo.getParValue())) {
				CateringCoupon bt = cateringCouponDao.selectOne(new LambdaQueryWrapper<CateringCoupon>().eq(CateringCoupon::getParValue, vo.getParValue()));
				if(bt != null && !bt.getSysId().equals(vo.getSysId())) return ResultData.error(ResultEnum.SAME_DATA.getCode(),"优惠卷“"+vo.getParValue()+"”"+ResultEnum.SAME_DATA.getMsg());
			}
			CateringCoupon as = cateringCouponDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringCouponDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}
	//商品优惠卷删除
	public ResultData cateringCouponDelete(Map map) {
		try {
			CateringCoupon vo = JSON.parseObject(JSON.toJSONString(map),CateringCoupon.class);
			CateringCoupon as = cateringCouponDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringCouponDao.deleteById(as.getSysId());
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
		}
	}

	//商品评价
	public ResultData cateringDiscussSelect(Map map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"t1");
			wrapper.orderByAsc("status");
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringDiscuss> page =new Page<CateringDiscuss>(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("limit").toString()));
				IPage<CateringDiscuss> pageList = cateringDiscussDao.selectPageList(page, wrapper);
				Integer count = cateringDiscussDao.selectPageCount(wrapper);
				List<CateringDiscuss> list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				List<CateringDiscuss> list = cateringDiscussDao.selectNoPageList(wrapper);
				return ResultData.success(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
		}
	}
	//新增商品评价
	public ResultData cateringDiscussInsert(Map map) {
		try {
			CateringDiscuss vo = JSON.parseObject(JSON.toJSONString(map),CateringDiscuss.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringDiscussDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}
	//商品评价修改
	public ResultData cateringDiscussUpdate(Map map) {
		try {
			CateringDiscuss vo = JSON.parseObject(JSON.toJSONString(map),CateringDiscuss.class);
			CateringDiscuss as = cateringDiscussDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringDiscussDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}
	//商品评价删除
	public ResultData cateringDiscussDelete(Map map) {
		try {
			CateringDiscuss vo = JSON.parseObject(JSON.toJSONString(map),CateringDiscuss.class);
			CateringDiscuss as = cateringDiscussDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringDiscussDao.deleteById(as.getSysId());
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
		}
	}

	//用户信息
	public ResultData cateringUserSelect(Map map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"");
			wrapper.orderByDesc("create_time");
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringUser> page =new Page<CateringUser>(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("limit").toString()));
				IPage<CateringUser> pageList = cateringUserDao.selectPage(page, wrapper);
				Integer count = cateringUserDao.selectCount(wrapper);
				List<CateringUser> list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				List<CateringUser> list = cateringUserDao.selectList(wrapper);
				return ResultData.success(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
		}
	}
	//新增用户信息
	public ResultData cateringUserInsert(Map map) {
		try {
			CateringUser vo = JSON.parseObject(JSON.toJSONString(map),CateringUser.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringUserDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}
	//商品用户信息
	public ResultData cateringUserUpdate(Map map) {
		try {
			CateringUser vo = JSON.parseObject(JSON.toJSONString(map),CateringUser.class);
			CateringUser as = cateringUserDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringUserDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}
	//商品用户信息
	public ResultData cateringUserDelete(Map map) {
		try {
			CateringUser vo = JSON.parseObject(JSON.toJSONString(map),CateringUser.class);
			CateringUser as = cateringUserDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringUserDao.deleteById(as.getSysId());
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
		}
	}

	//用户订单
	public ResultData cateringOrderSelect(Map<String,String> map) {
		return cateringOrderService.select(map);
	}
	//新增用户订单
	public ResultData cateringOrderInsert(Map map) {
		try {
			CateringOrder vo = JSON.parseObject(JSON.toJSONString(map),CateringOrder.class);
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringOrderDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}
	//修改用户订单
	public ResultData cateringOrderUpdate(Map map) {
		try {
			CateringOrder vo = JSON.parseObject(JSON.toJSONString(map),CateringOrder.class);
			CateringOrder as = cateringOrderDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringOrderDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}
	public ResultData cateringOrderUpdateStatus(Map<String,String> map) {
		return cateringOrderService.updateStatus(map);
	}
	//删除用户订单
	public ResultData cateringOrderDelete(Map map) {
		try {
			CateringOrder vo = JSON.parseObject(JSON.toJSONString(map),CateringOrder.class);
			CateringOrder as = cateringOrderDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),ResultEnum.REQ_DATA_ERROR.getMsg());
			cateringOrderDao.deleteById(as.getSysId());
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
		}
	}

	//用户订单详情
	public ResultData cateringOrderCartSelect(Map map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"t1");
			if(map.get("page") != null && map.get("page") != "") {
				IPage<CateringOrderCart> page =new Page<CateringOrderCart>(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("limit").toString()));
				IPage<CateringOrderCart> pageList = cateringOrderCartDao.selectPageList(page, wrapper);
				Integer count = cateringOrderCartDao.selectPageCount(wrapper);
				List<CateringOrderCart> list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				List<CateringOrder> list = cateringOrderCartDao.selectNoPageList(wrapper);
				return ResultData.success(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
		}
	}

	//用户购物车列表
	public ResultData cateringUserCartSelect(Map<String,String> map) {
		return cateringCartService.select(map);
	}
	//用户地址列表
	public ResultData cateringUserAddressSelect(Map<String,String> map) {
		return cateringAddressService.select(map);
	}
	//用户地址列表
	public ResultData cateringUserCouponSelect(Map<String,String> map) {
		return cateringUserCouponService.select(map);
	}
	//用户积分使用列表
	public ResultData cateringIntegralSelect(Map<String,String> map) {
		return cateringIntegralService.select(map);
	}
	//用户物流跟踪
	public ResultData cateringOrderLogSelect(Map<String,String> map) {
		return cateringOrderLogService.select(map);
	}
	public ResultData cateringFeedbackSelect(Map<String,String> map) {
		return cateringFeedbackService.select(map);
	}

	//系统模块
	public ResultData cateringModuleSelect(Map<String,String> map) {
		return cateringModuleService.select(map);
	}
	//新增系统模块
	public ResultData cateringModuleInsert(Map map) {
		return cateringModuleService.insert(map);
	}
	//修改系统模块
	public ResultData cateringModuleUpdate(Map map) {
		return cateringModuleService.update(map);
	}
	//删除系统模块
	public ResultData cateringModuleDelete(Map map) {
		return cateringModuleService.delete(map);
	}

}

